<?php
/**
 * +-------IMBA广告业务数据的获取操作--------+
 * +-------------------------------------------------+
 * +-------广告业务数据的内部操作接口---------------+
 * +-------------------------------------------------+
 * +-@author susy.liang <liangling@ilongyuan.com.cn>-+
 * +-------------------------------------------------+
 */
namespace Api\Event;

class AdEvent extends BaseEvent {

    /**
     * 模型对象
     */
    protected $mod;

    /**
     * 成员变量的初始化
     */
    protected function _initialize() {
        parent::_initialize();
        $this->mod = D( 'Common/Ad', 'Service' );
    }

    /**
     * 广告列表数据
     *
     * @param string  $adsCode 广告位的标识
     * @return array
     */
    public function lists( $adsCode = '' ) {
        if ( !$adsCode ) {
            return array();
        }
        $service = D( 'Common/Ads', 'Service' );
        $map = array(
            'code'=>$adsCode,
            'status'=>array( 'gt', 0 )
        );
        $res = $service->where( $map )->field( 'id' )->find();

        $map = array(
            'ads_id'=>$res['id'],
            'status'=>array( 'gt', 0 )
        );

        $data = $this->mod->field( 'id,name,image,link' )->where( $map )->order( 'sort ASC' )->find();
        uri_format($data['image']);
        return $data;
    }


    ############### FROM 2015-09-22 11:24:22 ############

    /** 
     * [根据标示获取广告下的广告数据]
     * @author Legend. <xcx_legender@qq.com>
     * @param 
     * @return 
     */
    public function getAdsByCode($code = '', $limit = 0)
    {
        $ads_id = $this->getAdsIdByCode($code);
        if (!$ads_id)
        {
            return [];
        }
        $map['ads_id'] = (int)$ads_id;
        $map['status'] = 1;
        $ads = service('Ad')->where($map)->order('sort asc')->select();
        return $ads;
    }


    protected function getAdsIdByCode($code = '')
    {
        $map['code'] = $code;
        $res = service('Ads')->where($map)->field('id')->find();
        return $res['id'];
    }


}